﻿using Microsoft.Web.Administration;
using Quartz;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoPublish.BackUpServices
{
    public class BackUpServices
    {


    }


    public class PublishJob : IJob
    {
        bool isLock = false;

        static object lockObj = new object();

        string programFolder = "C:\\Code\\gitee\\新建文件夹\\temp\\WebApplication1";
        string publishFolder = "C:\\webSite\\publishFolder";
        string iisWebName = "T";
        string iisFolder = "C:\\webSite\\dotnetTest";
        string appPoolName = "dotnetCore";

        public Task Execute(IJobExecutionContext context)
        {
            if (isLock)
            {
                return Task.CompletedTask;
            }

            lock (lockObj)
            {
                try
                {

                    CommandRunner git = new CommandRunner("git", programFolder);
                    string pullResult = git.Run("pull");

                    if (pullResult.ToLower().Contains("already up to date"))
                    {
                        return Task.CompletedTask;
                    }

                    ServerManager serverManager = new ServerManager();
                    var site = serverManager.Sites[iisWebName];
                    ApplicationPool appPool = serverManager.ApplicationPools[appPoolName];
                    CommandRunner dotnet = new CommandRunner("dotnet", programFolder);

                    string r = "";
                    r = dotnet.Run("publish -c Release -o " + publishFolder);
                    File.Delete(publishFolder + "\\appsettings.json");

                    string tempWebFilePath = iisFolder + "/app_offline.htm";

                    File.WriteAllText(tempWebFilePath, "Under Maintenance");
                    Thread.Sleep(5000);

                    //if (site.State == ObjectState.Started)
                    //    site.Stop();
                    //if (appPool.State == ObjectState.Started)
                    //    appPool.Stop();
                     serverManager.CommitChanges();

                    CopyDirectory(publishFolder, iisFolder);

                    File.Delete(tempWebFilePath);

                    //appPool.Start();
                    //site.Start();
                    //serverManager.CommitChanges();
                }
                catch (Exception e)
                {
                }
                finally
                {
                    isLock = false;
                }
            }
            return Task.CompletedTask;
        }

        void CopyDirectory(string srcDir, string destDir)
        {
            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }

            foreach (string file in Directory.GetFiles(srcDir))
            {
                string dest = Path.Combine(destDir, Path.GetFileName(file));
                File.Copy(file, dest, true);
            }

            foreach (string folder in Directory.GetDirectories(srcDir))
            {
                string dest = Path.Combine(destDir, Path.GetFileName(folder));
                CopyDirectory(folder, dest);
            }
        }
    }
}
